$('#addBirth').css('height',($(window).height())+'px');
$("#add").click(function () {
    console.log("add birth..")
    $("#addBirth").css('display','block');
    $(".addBirth-form").show();
});
//判断专家是否已经配置服务项目和价格信息
var teacherdetail_hassimpleconfig=0,teacherdetail_hasmultipleconfig=0;
//选择地区
Area.init("#area_province","#area_city","#area_area");

function menu(e) {

    if(e==3){
        console.log(3)
        $("#serviceType").val(3);
        // $("#simple").css('display','block');
        $("#multiple").css('display','none');
        // $("#b_simple").css('color','#e04d3e');
        // $("#b_simple").css('background-color','#fbe6e3');
        // $("#cb_simple").prop("checked",true);
        $("#b_multiple").css('color','#333333');
        $("#b_multiple").css('background-color','#ffffff');
        $("#cb_multi").prop("checked",false);
        $("#b-image").attr("src","img/teacher/t-7.png");
    }else if(e==1){
        console.log(1)
        $("#cb_eval").prop("checked",false);
        $("#evaluate").css('display','none');
        $("#b_eval").css('color','#333333');
        $("#b_eval").css('background-color','#ffffff');


        $("#serviceType").val(1);
        // $("#simple").css('display','none');
        $("#multiple").css('display','block');
        $("#b_multiple").css('color','#e04d3e');
        $("#b_multiple").css('background-color','#fbe6e3');
        $("#cb_multi").prop("checked",true);
        // $("#b_simple").css('color','#333333');
        // $("#b_simple").css('background-color','#ffffff');
        // $("#cb_simple").prop("checked",false);
        $("#b-image").attr("src","img/teacher/t-7.png");
    }else if(e==2){
        // pop_up("功能开发中");
        $("#cb_multi").prop("checked",false);
        $("#multiple").css('display','none');
        $("#b_multiple").css('color','#333333');
        $("#b_multiple").css('background-color','#ffffff');

        $("#serviceType").val("");
        $("#cb_eval").prop("checked",true);
        $("#b_eval").css('color','#e04d3e');
        $("#b_eval").css('background-color','#fbe6e3');
        $("#evaluate").css('display','block');


    }else if(e==0){
        pop_up("功能正在开发中，请稍后！")
    }
};

//关注
function  guanzhu(){
    var teacherid = server.getQueryString("teachid");
    if(server.isEmpty(teacherid)){
        pop_up('丢失专家ID');
        return;
    }
    server.req('wechat/fan/save',{teacherId:teacherid},function (res) {
        // console.log(res)
        if(res.code==0){
            $("#divguanzhu").css("display","none");
            $("#divguanzhu2").css("display","block");
            pop_up('关注成功');
        }
    },null,"POST")
}
//查询是否关注
function queryGuanzhu(){
    var teacherid = server.getQueryString("teachid");
    if(server.isEmpty(teacherid)){
        pop_up('丢失专家ID');
        return;
    }
    // console.log(teacherid)
    server.req('wechat/fan/queryinfo',{teacherId:teacherid},function (res) {
        // console.log("guanzhu list")
        // console.log(res)
        if(res.code==0){
            if(res.data){
                $("#divguanzhu").css("display","none");
                $("#divguanzhu2").css("display","block");

            }
        }
    })
};
//取消关注
function CancelGZ(){
    var teacherid = server.getQueryString("teachid");
    if(server.isEmpty(teacherid)){
        pop_up('丢失专家ID');
        return;
    }
    server.req('wechat/fan/CancelGZ',{teacherId:teacherid},function (res) {
        console.log("cancle list")
        console.log(res)
        if(res.code==0){
                $("#divguanzhu").css("display","block");
                $("#divguanzhu2").css("display","none");
                pop_up('已取消关注');
                // console.log(window.location.href)
        }
    })
}

function savebirth(id){
    if(id==1){
        var obj=$("#tearchdetail_frm").serializeObject();
        console.log(obj);
        if(server.isEmpty(obj.name)||server.isEmpty(obj.birthday)||server.isEmpty(obj.sex)||server.isEmpty(obj.province)||server.isEmpty(obj.city)||server.isEmpty(obj.area)){
            pop_up("信息输入不完整")
            return;
        }
        var birth = {};
        birth.name = obj.name;
        birth.birthday  = new Date(obj.birthday).format("yyyy-MM-dd hh:mm:ss");
        birth.birthCity = obj.city;
        birth.birthXian = obj.area;
        birth.birthProvince = obj.province;
        birth.birthCountry = "中国";
        birth.sex = obj.sex;

        console.log(birth)
         server.req('wechat/birth/save',birth,function (e) {
             console.log(e);
             if(e.code==0){
                 $("#addBirth").css('display','none');
                 $(".addBirth-form").hide();
                 server.loadindex('page/teacher/teacherdetail/teacherdetail',window.location.search.substring(1))
             }else{
                 pop_up(e.msg);
             }
         },null,"POST")
    }else if(id==2){
        $("#addBirth").css('display','none');
        $(".addBirth-form").hide();
    }
};
//删除Birth
function delBirth(id){
    server.req('wechat/birth/delete/'+id,{},function(e){
        if(e.code==0){
            server.loadindex('page/teacher/teacherdetail/teacherdetail',window.location.search.substring(1))
        }else{
            pop_up(e.msg);
        }
    },null,"POST");
};
function selectBirth(id,index,total){
// console.log(index + "-" +total);
    $("#birthid").val(id);
    for(var i=0;i<total;i++){

        if(i==index){
            $("#birth"+i).removeClass();
            $("#birth"+i).addClass('births-item2-selected');
            // console.log("set selected "+index+"-"+i);
        }else{
            // console.log("set nonselected "+index+"-"+i);
            $("#birth"+i).removeClass();
            $("#birth"+i).addClass('births-item2');
        }

    }
};
var memberid;
function tochat(){
    server.loadindex('page/chat/chat','teacherTopicID='+memberid);
}


function teacherdetail_submitorder(){

    var  obj ;
    obj={};
    var serviceType= $("#serviceType").val();
    obj=$("#tearchdetail_frm").serializeObject();

    if(!serviceType){
        pop_up("请选择问题类型");
        return;
    }
    if(serviceType == "3"){
        if(teacherdetail_hassimpleconfig==0){
            pop_up("当前专家还未定义简单问题，不能咨询");
            return;
        }
        if(server.isEmpty(obj.birthid)){
            pop_up("选择您的出生时间及地点");
            return;
        }
        if(server.isEmpty(obj.description)){
            pop_up("请描述问题详情");
            return;
        }
        if(server.isEmpty(obj.agree)){
            pop_up("请同意业务须知");
            return;
        }
    }else if(serviceType=="1"){
        console.log(obj.service)
        if(teacherdetail_hasmultipleconfig==0){
            pop_up("当前专家还未设置高级问题，不能咨询");
            return;
        }
        if(!obj.service){
            pop_up("请先选择所需服务");
            return;
        }
    }else if(serviceType=="0"){
        if(!obj.service ){
            pop_up("没有选择服务项目");
            return;
        }
    }

    //判断当前用户是否会员，否则拦截
    server.getUserInfo(function (info) {
        console.log(info)
        if(info.memberLevel==0){
            pop_up("只有会员才能购买大师咨询项目，请开通会员");
            server.loadindex('page/user/vipbuy/vipbuy');
            return;
        }
    })


    var teacherid = server.getQueryString("teachid");
   // obj.serviceType=obj.serviceType;
    obj.teacherId=teacherid;
    var item= {};
    obj.itemList=[];

    if(serviceType=="3") {
        item = {
            itemType: obj.serviceType,
            itemName: obj.description,
            description: obj.description,
            birthId: obj.birthid,
            openQuestions: (obj.publicask && obj.publicask == "on") ? 1 : 0
        };
    }else if(serviceType=="1")  {
        item = {
            itemType: obj.serviceType,
            itemId:obj.service

        };
    }
   //  itemList.push(item);
    obj.itemList.push(item);
    // console.log(obj)
    server.req("wechat/order/createOrder",obj,function (e){
        // console.log(e);
        if(e.code==0){
            server.setCache("orderInfo",JSON.stringify(e.data));
            server.loadindex('page/order/submitorder/submitorder',"",true);
        }else{
            pop_up(e.msg)
        }
    },null,"POST");

    //防止重复点击提交订单
    $("#teacherdetailsubmit").removeAttr("onclick");
};
//根据评分，得到小星星的数量
function scoreImage(score) {
    var himage="";
    if(score){
        if(score==0||(score>0&&score<1)){
            himage='<img src="img/teacher/4-5.png" class="star"><img src="img/teacher/4-5.png" class="star"><img src="img/teacher/4-5.png" class="star"><img src="img/teacher/4-5.png" class="star"><img src="img/teacher/4-5.png" class="star">';
        }else if(score==1||(score>1&&score<2)){
            himage='<img src="img/teacher/4-4.png" class="star"><img src="img/teacher/4-5.png" class="star"><img src="img/teacher/4-5.png" class="star"><img src="img/teacher/4-5.png" class="star"><img src="img/teacher/4-5.png" class="star">';
        }else if(score==2||(score>2&&score<3)){
            himage='<img src="img/teacher/4-4.png" class="star"><img src="img/teacher/4-4.png" class="star"><img src="img/teacher/4-5.png" class="star"><img src="img/teacher/4-5.png" class="star"><img src="img/teacher/4-5.png" class="star">';
        }else if(score==3||(score>3&&score<4)){
            himage='<img src="img/teacher/4-4.png" class="star"><img src="img/teacher/4-4.png" class="star"><img src="img/teacher/4-4.png" class="star"><img src="img/teacher/4-5.png" class="star"><img src="img/teacher/4-5.png" class="star">';
        }else if(score==4||(score>4&&score<5)){
            himage='<img src="img/teacher/4-4.png" class="star"><img src="img/teacher/4-4.png" class="star"><img src="img/teacher/4-4.png" class="star"><img src="img/teacher/4-4.png" class="star"><img src="img/teacher/4-5.png" class="star">';
        }else if(score>=5){
            himage='<img src="img/teacher/4-4.png" class="star"><img src="img/teacher/4-4.png" class="star"><img src="img/teacher/4-4.png" class="star"><img src="img/teacher/4-4.png" class="star"><img src="img/teacher/4-4.png" class="star">';
        }
    }else{
        himage='<img src="img/teacher/4-5.png" class="star"><img src="img/teacher/4-5.png" class="star"><img src="img/teacher/4-5.png" class="star"><img src="img/teacher/4-5.png" class="star"><img src="img/teacher/4-5.png" class="star">';
    }
    return himage;
};
//会员协议
function toagree(key) {
    server.loadindex('page/agree/agree','key='+key);
};

//去打赏
function todashang(){
    // var tid = server.getQueryString("teachid");
    // server.loadindex('page/teacher/dashang/dashang','teacherid='+tid,false);
    pop_up('功能开发中');
};
$(function (e) {
    //检索当前会员是否关注了当前专家；
    queryGuanzhu();
    menu(1);

    var teacherid = server.getQueryString("teachid");
    if(server.isEmpty(teacherid)){
        pop_up("未获取到专家ID，请与管理员联系");
        reutnr;
    }
    $("#b_simple").css('color','red');
    // menu(1);
    // console.log(teacherid)

    server.req('wechat/teacher/info/'+teacherid,{},function (e) {
        console.log(e)
        // console.log("memberid = "+e.data.memberId);
        memberid =e.data.memberId;
        // $("#bg_detault").attr("src",e.data.)
        if(e.code==0){
            var data = e.data;
            try{
            // document.getElementById("banner").style.backgroundImage='url('+(data.bgImage)+')';
            }catch (e) {  }
            $("#teacherimage").attr('src',data.headimgurl);
            console.log("height="+$("#banner").height());
            $("#teacherimage").css('height',($("#banner").height()-50)+'px')
            $("#nickname").text(data.nickname||"");
            $("#fans").text("粉丝数："+(data.fansNumber||0));
            if(data.skills){
                var sp;
                if(data.skills.indexOf(";")!=-1){
                    sp = data.skills.split(";");
                }else if(data.skills.indexOf(",")!=-1){
                    sp = data.skills.split(",");
                }else{
                    sp = data.skills;
                }

                var sH = "";
                if(sp) {
                    if( typeof  sp== "string"){
                        sH = sH + '<div class="banner-right-line3-item" style=" "><label>' + sp + '</label></div>';
                        $("#skills").html(sH);
                    }else{
                        sp.forEach(function (value, index1) {
                            if(index1==0){
                                sH = sH + '<div class="banner-right-line3-item" style=" "><label>' + value + '</label></div>'
                            }else{
                                sH = sH + '<div class="banner-right-line3-item"><label>' + value + '</label></div>'
                            }

                        });
                        $("#skills").html(sH);
                    }
                }else{
                    sH = '<div class="banner-right-line3-item-nodata"><label>(专家未设置)</label></div>'
                    $("#skills").html(sH);
                }
            }

            $("#ask_number").text((data.askNumber||0));
            // $("#evaluate_score").text("综合评分："+(data.evaluateNumber||0));
            //根据得分多少显示小星星
            var himage = scoreImage(data.evaluateScore);
            data.himage = himage;
            $("#himage").html(himage);

            // $("#workyears").text("从业年限:"+(data.workYears||1));
            $("#desc").text(data.description||'');

            server.loadData("#banner",e.data);
            server.loadData(".description",e.data);
        }


    });
    server.req('wechat/teacher/serversByTearchId',{tearcherId:teacherid},function (e) {
        console.log("---teacher services---")
        console.log(e)
        var strH = '<div class="nodata">\n' +
            '                <div class="nodata-image">\n' +
            '                <img src="img/icons/search.png" />\n' +
            '                </div>\n' +
            '                <div class="nodata-text">\n' +
            '                <label>没有找到任何数据</label>\n' +
            '                </div>\n' +
            '            </div>';
        if(e.code==0){
            strH = '<div class="multiple-tip"><img src="img/teacher/t-8.png" />老师推荐</div><div class="multiple-notice">以下服务项目服务时间为5天</div>';
            var data = e.data;
            //判断是否有服务项目的配置信息
            if(data!=null)
                teacherdetail_hasmultipleconfig=1;
                data.forEach(function (item,index) {
                    var serviceImg = item.image;
                    if(server.isEmpty(serviceImg))
                        serviceImg = "img/icons/default.png";
                    serviceImg=item.image;
                    strH = strH + '<div class="multiple-item">\n' +
                        '                <div class="multiple-item-left">\n' +
                        '                    <img src="'+serviceImg+'" />\n' +
                        '                </div>\n' +
                        '                <div class="multiple-item-middle">\n' +
                        '                    <label>'+item.title+' ¥'+item.discountPrice+'/次</label>\n' +
                        '                </div>\n' +
                        '                <div class="multiple-item-right">\n' +
                        '                    <input type="radio" name="service" value="'+item.itemId+'">\n' +
                        '                </div>\n' +
                        '            </div>';
                });
            // $("#multiple").html(strH);
            server.loadData("#multiple",e);
        }
    });

    //加载当前专家的配置信息
server.req('wechat/teacherconfig/getByTeacherId',{teacherId:teacherid},function (res) {

    // console.log("teacher config.."+teacherid);
    // console.log(res);
    if(res.data!=null){
        teacherdetail_hassimpleconfig=1;
        $("#simpleprice").text(res.data.askPrice);
    }else{
        $("#simpleprice").text('-专家未定义-');
    }

})

    //加载当前用户的Birth列表
    server.req('wechat/birth/list',{},function (e) {
        var strBirthHtml = '';
        if(e.code==0){
            console.log("birthes:")
            console.log(e);
            var birthList = e.data;
            var birthNum = e.size;
            if(birthList[0])
                $("#birthid").val(birthList[0].id);
                birthList.forEach(function(item,index){
                    // console.log("index:"+index)
                    if(index==0){
                        strBirthHtml = strBirthHtml+ '<div class="births-item2-selected" id="birth'+index+'" onclick="selectBirth('+item.id+','+index+','+birthNum+')">\n' +
                            '                        <label>'+item.name+'</label><img id="delbirth'+index+'" onclick="delBirth('+item.id+')" src="img/teacher/t-10.png" />' +
                            '                    </div>' ;
                    }else{
                        strBirthHtml = strBirthHtml+ '<div class="births-item2" id="birth'+index+'" onclick="selectBirth('+item.id+','+index+','+birthNum+')">\n' +
                            '                        <label>'+item.name+'</label><img id="delbirth'+index+'" onclick="delBirth('+item.id+')" src="img/teacher/t-10.png" />' +
                            '                    </div>' ;
                    }

                })
                $("#births").html(strBirthHtml);
        }
    });


    //加载当前专家的评价信息
    server.req('wechat/orderevaluate/getByTeacherId',{teacherId:teacherid},function (res) {
        if(res.code==0){
            res.data.forEach(function (item,index){
                //设置星星的数量
                $("#evalcount").text(res.data.length);
                var strStar="";
                if(item.score){
                    for(var j=0;j<item.score;j++){
                        strStar=strStar+'<img src="img/teacher/star@l.png" />';
                    }
                    for(var k=0;k<5-item.score;k++){
                        strStar=strStar+'<img src="img/teacher/star1@l.png" />';
                    }
                }
                res.data[index].strStar = strStar;
            })
            server.loadData(".evaluate",res);
        }
    })
});
